package com.example.vue20220402.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;

/**
 * @Description
 * @Autor 伍军
 * @Date 2021/9/15 13:58
 * @Version 1.0
 **/
@Component
public class MyEmail {

    //注入邮件发送类
    @Resource
    JavaMailSender javaMailSender;

    @Value("${spring.mail.username}")
    private String formEmail;
    /**
     * 发送邮件
     * @param email 收件人的邮箱
     * @param content 邮件正文
     * @return
     */
    public HashMap<String,Object> sendEmail(String email,String content,String title){
        HashMap<String,Object> map = new HashMap<String,Object>();

        try{
            //创建一个邮件
            SimpleMailMessage message = new SimpleMailMessage();
            //写入收件人邮箱
            message.setTo(email);
            //写入发件人邮箱
            message.setFrom(formEmail);
            //写入邮件标题
            message.setSubject(title);
            //写入邮件正文
            message.setText(content);
            //发送邮件
            javaMailSender.send(message);
            map.put("info","发送成功");
        }catch(Exception e){
            e.printStackTrace();
            map.put("info","发送失败");
        }
        return map;
    }

}
